# -*- coding: utf-8 -*-

from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from pybo.myproblems.models import Problem

# Create your models here.

class Lesson(models.Model):
    title = models.CharField('Заголовок', max_length = 200)
    author = models.ForeignKey(User, name='Автор')
    order = models.PositiveIntegerField()
    depend = models.ManyToManyField("self", symmetrical = False, blank = True) 

    def __le__(lesson1, lesson2):
        return lesson1.id < lesson2.id

    def __leq__(lesson1, lesson2):
        return lesson1.id <= lesson2.id

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['id', ]

class LessonNode(models.Model):
    lesson = models.ForeignKey(Lesson, related_name="%(app_label)s_%(class)s_related")
    order = models.PositiveIntegerField()
    text = models.TextField(blank = True)
    problem = models.ForeignKey(Problem, blank = True, null = True)
    show_title = models.BooleanField()
    show_statement = models.BooleanField()


    def __unicode__(self):
        return self.lesson.title + "." + str(self.order) + ". " + self.text[:60] + "... (problem #" + (str(self.problem.id) if self.problem else '---') + ')'

    class Meta:
        ordering = ['lesson', 'order', ]

TYPE_CHOICES = (
    (1, 'доступен'),
    (2, 'завершен')
)

class LessonResult(models.Model):
    user = models.ForeignKey(User)
    lesson = models.ForeignKey(Lesson)    
    status = models.IntegerField(choices=TYPE_CHOICES)    

    def __unicode__(self):
        #FIXME add user firstname and lastname
        return self.lesson.title + " - " + self.user.username
